﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;
namespace Controller
{
    public class SettingController:ControllerBase
    {
        public SettingController()
        {
            
        }
        public SettingController(SkyfabDataContextDataContext sdc)
        {
            Sdc = sdc;
        }

        public int findLargestVendorPONO()
        {
            Setting largestVendorPONO = (from s in Sdc.Settings
                                         where s.Name == "LargestVendorPONO"
                                         select s).FirstOrDefault();
            return Convert.ToInt32((TransferSettingToSettingEntity(largestVendorPONO).Value));
        }

        public void updateLargestVendorPONO()
        {
            Setting largestVendorPONO = (from s in Sdc.Settings
                                         where s.Name == "LargestVendorPONO"
                                         select s).FirstOrDefault();
            int newValue = Convert.ToInt32(largestVendorPONO.Value) + 1;
            largestVendorPONO.Value = newValue.ToString();
            
            Sdc.SubmitChanges();
        }

        private SettingEntity TransferSettingToSettingEntity(Setting setting)
        {
            SettingEntity settingEntity = new SettingEntity()
                                              {
                                                  Id = setting.Id,
                                                  Name = setting.Name,
                                                  Value = setting.Value,
                                                  Comment = setting.Comment,
                                              };
            return settingEntity;
        }
    }
}
